//tslint:disable
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { Component, OnInit, Input } from '@angular/core';
import { AlertController, ModalController } from '@ionic/angular';
import { NestedFormTagComponent } from '../nested-form-tag/nested-form-tag.component';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { ChatService } from 'src/app/shared/service/chatService';
import { EventService } from 'src/app/shared/service/event.service';

@Component({
  selector: 'app-adviser-info',
  templateUrl: './adviser-info.component.html',
  styleUrls: ['./adviser-info.component.scss'],
})
export class AdviserInfoComponent implements OnInit {
  @Input() tagData: any;
  appTypeCustom: any;
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private httpService: HttpService,
    private alertController: AlertController,
    private chatService: ChatService,
    private eventService: EventService,
    public modalController: ModalController
  ) { }

  ngOnInit() {
    const bodyStyles = window.getComputedStyle(document.body);
    this.appTypeCustom = bodyStyles.getPropertyValue('--appTypeCustom').trim().replace('"', '').replace('"', '');
    //   this.data = {
    //     "rightLabel":"帮您快速找到适合店铺",
    //     title: "转店顾问",
    //     "phone":"10086",
    //     "phoneIcon":"expert_icon_phone@3x.png",
    //     "service":"成功服务1000位客户",
    //     "downLabel":"专业评估、筛选推荐、撮合议价",
    //     // "more":{
    //     //     "icon":"details_icon_more@3x.png",
    //     //     "link":{
    //     //         type:"page",
    //     //         "params":{
    //     //             "aa":"123"
    //     //         },
    //     //         "target":"160887"
    //     //     },
    //     //     "label":"更多顾问"
    //     // },
    //     "imageUrl":"assets/img/headtrait.png",
    //     "name":"昵称",
    //     "tag":"选址顾问"
    // }

    if (this.tagData.data.cssType === '2') {
      // 使用二维数组
      this.getDemoData(this.tagData)
    }


  }
  moreClick(link) {
    this.appGlobalService.openPage(link);
  }
  callClick(data) {
    this.presentAlertConfirm(data.phone, data.name);
  }
  chatClick(userId, hiddenAddBtn?) {
    if (this.tagData.data && this.tagData.data.gotoEnterpriseWechat) {
      const tmpLink = this.tagData.data.gotoEnterpriseWechat.link;
      if (tmpLink && tmpLink.params && tmpLink.params.targetUrl) {
        tmpLink.target = tmpLink.params.targetUrl;
      }
      this.pageModalService.openPage(tmpLink);
    } else {

      const paramsToCheck = [
        { key: 'chatUserId', storageKey: 'enterChatRoomByUserId' },
        { key: 'chatId', storageKey: 'enterChatRoomByUserId' },
        { key: 'chatMobile', storageKey: 'enterChatRoomByUserId' },
        { key: 'userId', storageKey: 'enterChatRoomByUserId' }
      ];

      let isValid = paramsToCheck.some(param => this.tagData.data[param.key] && this.tagData.data[param.key] !== '');
      // 1.6.34 如果用户要聊天并且配置了用于websocket的手机号码则更新手机号码
      if (isValid) {
        const userId = this.tagData.data.chatId || this.tagData.data.chatUserId || this.tagData.data.chatMobile || this.tagData.data.userId;
        if (this.chatService.getAvatar(userId)) {
          this.eventService.eventEmit.emit('enterChatRoomByUserId',
            {
              userId: userId,
              fromFriendList: true,
              friendName: this.chatService.nickNames[userId],
              friendAvatar: this.chatService.getAvatar(userId)
            });
        } else {
          this.httpService.get(
            "message/userinfo/im/" + userId,
            response => {
              const data = response.data;
              if (data) {
                if (data.avatar_url === null) { // 没头像给默认头像
                  data.avatar_url = this.appGlobalService.defaultImg;
                  this.chatService.setAvatar(userId, this.chatService.defaultBase64String);
                } else {
                  this.appGlobalService.getBase64Str(data.avatar_url + "?x-oss-process=image/resize,h_36", str => {
                    this.chatService.setAvatar(userId, str);
                  });
                }
                this.chatService.nickNames[userId] = data.nickname;
                this.eventService.eventEmit.emit('enterChatRoomByUserId',
                  {
                    userId: userId,
                    fromFriendList: true,
                    friendName: data.nickname,
                    friendAvatar: data.avatar_url
                  });
              }
            });

        }
      }
    }

  }
  downloadClick(link) {
    if (link) {
      this.appGlobalService.openPage(link);
    }
  }

  openPage(link, e) {
    if (e) {
      e.stopPropagation();
    }
    if (link.link && link.link !== undefined) {
      this.pageModalService.openPage(link.link);
    }
  }

  async copyClick(e, str, msg) {
    let that = this;
    // this.appGlobalService.copyStr(e, str, msg);
    let resScript = {
      type: "page",
      style: "page.DefaultPageLogic",
      titleBar: {},
      disableBack: false,
      menus: {},
      isGrayBg: false,
      views: [
        {
          type: "view",
          style: "view.DefaultViewLogic",
          isGrayBg: false,
          showWaterImg: false,
          tags: [
            {
              type: "tag",
              style: "tag.AppSimpleDetailTagLogic",
              name: null,
              linearLayout: "body",
              data: that.tagData.data
            },
          ]
        }
      ]
    }

    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'recommend-modal',
      component: NestedFormTagComponent,
      componentProps: { pageData: resScript, cssClass: 'recommend-modal', cssStyle: that.tagData.data.modalCssStyle }
    });
    await modal.present();
    const modalData = await modal.onDidDismiss().then((data) => {
    });
  }

  async presentAlertConfirm(mobile, name?) {
    const msg = (name ? name + ' ' : '') + mobile;
    const alert = await this.alertController.create({
      header: this.tagData.nickName,
      message: msg,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary"
        },
        {
          text: "拨打",
          handler: () => {
            this.httpService.changeCallNumber(mobile);
          }
        }
      ]
    });

    await alert.present();
  }

  getDemoData(tagData) {

    // <dataItem name="step1Icon" value="https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_163546934823369532d11c5a117316ed10e3995bc1717.png"></dataItem>
    // <dataItem name="step2Icon" value="https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_1635469397399078ad520540e9cc7a02634865b78c71f.png"></dataItem>
    // <dataItem name="step3Icon" value="https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_1635469431784fbec0b7b31e14227de5c716d3dcff34f.png"></dataItem>
    // <dataItem name="step4Icon" value="https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_163546946039429c33aee9ae9014bdf9a8c2494ba1fb1.png"></dataItem>
    this.tagData.data.type2Array =
      [
        {
          stepIcon: tagData.data.step1Icon ? tagData.data.step1Icon : "https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_163546934823369532d11c5a117316ed10e3995bc1717.png",
          stepIconCssStyle: tagData.data.step1IconCssStyle ? tagData.data.step1IconCssStyle : "width:1.75rem;height:1.75rem;",
          title: "装修准备阶段",
          data: [
            { subTitle: "找设计师", comment: "确定新风空调橱柜、厨房电器、热水器、洗手台、马桶、花洒、洗衣机、电视、电视柜、沙发、床、床头柜等的尺寸和位置，以便规划开关插座和上下水位置" },
            { subTitle: "找施工方", comment: "考察意向施工方的工地" },
            { subTitle: "量房", comment: "量房" },
            { subTitle: "购买材料", comment: "定中央空调新风地暖或暖气、橱柜、热水器、门窗、铝扣板吊顶等" }
          ]
        },
        {
          stepIcon: tagData.data.step2Icon ? tagData.data.step2Icon : "https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_1635469397399078ad520540e9cc7a02634865b78c71f.png",
          stepIconCssStyle: tagData.data.step2IconCssStyle ? tagData.data.step2IconCssStyle : "width:1.75rem;height:1.75rem;",
          title: "硬装（基础施工）阶段", data: [
            { subTitle: "开工交底", days: "半天", comment: "业主、设计师、工长(项目经理)、监理到场监理检查原房状况提出整改建议" },
            { subTitle: "拆除", days: "3-5天" },
            { subTitle: "室内结构改造", days: "3-5天", comment: "新建、改建墙体" },
            { subTitle: "水电交底", days: "半天", comment: "业主、设计师、工长(项目经理)水电工、监理到场，就中央空调新风、地暖等交底；也可就热水器软水机净水器等，到场测量防盗门/测量室内门/测量窗户/测量橱柜；监理确定水电点位和走向。" },
            { subTitle: "水电改造", days: "3-7天", comment: "在水电施工前，安装中央空调；在水电施工后，安装地暖，然后整体回填；燃气表移位/改暖气管；空调、热水器打孔、安装窗户(在瓦工进场前为宜)；监理检查水电隐蔽施工，包括水电材料、线盒安装、灯头盒安装、强弱电线管施工、湿区地面开槽防水等" },
            { subTitle: "水电验收", days: "半天", comment: "报据水电工列的单子买开关插座面板、角阀、地漏等安装防盗门(在瓦工进场前为宜)；监理验收水电限薇施工包括给水排水管安装、电线管和线盒安装、穿线、配电箱安装、各管道排设间距、循环热水管安装等" },
            { subTitle: "填槽、做保温、做防水", days: "2天" },
            { subTitle: "做吊顶、垭口、背景墙等木饰", days: "3-7天", comment: "监理检查瓦/木基础施工，包括墙地面贴砖基层、墙面防水施工、吊顶龙骨施工、轻质隔墙基础施工等" },
            { subTitle: "贴墙砖", days: "3-4天", comment: "厨房贴墙砖之前，安装烟道止逆阀和烟管；安装窗台石(最晚在刮腻子前)；复尺测量橱柜；监理检查瓦/木面层施工，包括墙面砖铺贴、石膏板面板施工、木基层板施工等" },
            { subTitle: "闭水实验", days: "3-5天", comment: "提前和楼下邻居打好招呼将要做闭水试验，留下联系方式,可能去楼下查看是否漏水；选地板，根据地面确认可选的地板厚度；安装过门石(最晚和地砖同时铺装)；监理验收闭水试验包括地面防水施工、挡水条安装、闭水试验结果等" },
            { subTitle: "贴地砖及地面水泥找平", days: "3-4天", comment: "改燃气管/测量厨卫吊顶；贴完砖一周后即可用勾缝剂填缝；监理中期验收，包括石膏板吊顶施工、石膏线安装、木饰造型施工、木线条施工、墙地砖铺贴、填缝等" },
            { subTitle: "刮腻子2~3遍", days: "3-5天" },
            { subTitle: "腻子打磨", days: "1-2天", comment: "监理检查腻子批刮和打磨" },
            { subTitle: "刷乳胶漆或贴墙纸、墙布", days: "2-3天", comment: "安装厨卫吊顶开关插座面板、灯具；有颜色的乳胶漆一般先刷底漆，安装开关插座面板后，再刷面漆；监理检查乳胶漆施工、分色线处理" },
            { subTitle: "竣工验收", days: "半天", comment: "监理验收开关播座灯具安装、强弱电箱安装、龙头角阀和软管安装、乳胶漆施工、分色线处理等" }
          ]
        },
        {
          stepIcon: tagData.data.step3Icon ? tagData.data.step3Icon : "https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_1635469431784fbec0b7b31e14227de5c716d3dcff34f.png",
          stepIconCssStyle: tagData.data.step3IconCssStyle ? tagData.data.step3IconCssStyle : "width:1.75rem;height:1.75rem;",
          title: "主材安装阶段", data: [
            { subTitle: "橱柜安装", days: "1天", comment: "可以约油烟机、灶具、其他厨房电器一起上门安装" },
            { subTitle: "室内门安装", days: "1天", comment: "在橱柜之后安装，避免大板子进出碰坏门" },
            { subTitle: "地板安装", days: "1天", comment: "提前10天约商家测量和验收地面，看是否具备铺装条件" },
            { subTitle: "定制家具安装", days: "1天" },
            { subTitle: "卫浴安装", days: "1天", comment: "花洒、浴缸、马桶等安装" },
            { subTitle: "墙面修补", days: "1天", comment: "退掉多余瓷砖/木门及其他主材修补/开荒保洁" }
          ]
        },
        {
          stepIcon: tagData.data.step4Icon ? tagData.data.step4Icon : "https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_163546946039429c33aee9ae9014bdf9a8c2494ba1fb1.png",
          stepIconCssStyle: tagData.data.step4IconCssStyle ? tagData.data.step4IconCssStyle : "width:1.75rem;height:1.75rem;",
          title: "软装阶段", data: [
            { subTitle: "进家具" },
            { subTitle: "晾味", comment: "空房晾味至少1个月，尤其是橱柜、家具等木制品的气味都散去后再入住。家里有小孩的，最好晾味时间长一点比如经过一个夏天。" }
          ]
        },
        {
          title: "入住",
          stepIcon: tagData.data.step5Icon ? tagData.data.step5Icon : "https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_163546946039429c33aee9ae9014bdf9a8c2494ba1fb1.png",
          stepIconCssStyle: tagData.data.step4IconCssStyle ? tagData.data.step4IconCssStyle : "width:1.75rem;height:1.75rem;"
        },
      ];
  }
}


